Completed
Push — master ( 40c2b4...09bd06 )
by Justin
01:29
created

Event.isInstance   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
var Subject = require('./Subject'),
2
    GDate = require('./Date'),
3
    PlaceReference = require('./PlaceReference'),
4
    EventRole = require('./EventRole'),
5
    utils = require('./utils');
6
7
/**
8
 * An event.
9
 * 
10
 * @constructor
11
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
12
 */
13
var Event = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Event)){
17
    return new Event(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Event.isInstance(json)){
22
    return json;
23
  }
24
  
25
  Subject.call(this, json);
26
  
27
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
28
    this.setType(json.type);
29
    this.setDate(json.date);
30
    this.setPlace(json.place);
31
    this.setRoles(json.roles);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
32
  }
33
};
34
35
Event.prototype = Object.create(Subject.prototype);
36
37
Event._gedxClass = Event.prototype._gedxClass = 'GedcomX.Event';
38
39
/**
40
 * Check whether the given object is an instance of this class.
41
 * 
42
 * @param {Object} obj
43
 * @returns {Boolean}
44
 */
45
Event.isInstance = function(obj){
46
  return utils.isInstance(obj, this._gedxClass);
47
};
48
49
/**
50
 * Get the type
51
 * 
52
 * @returns {String}
53
 */
54
Event.prototype.getType = function(){
55
  return this.type;
56
};
57
58
/**
59
 * Set the type
60
 * 
61
 * @param {String} type
62
 * @returns {Event}
63
 */
64
Event.prototype.setType = function(type){
65
  this.type = type;
66
  return this;
67
};
68
69
/**
70
 * Get the date
71
 * 
72
 * @returns {Date}
73
 */
74
Event.prototype.getDate = function(){
75
  return this.date;
76
};
77
78
/**
79
 * Set the date
80
 * 
81
 * @param {Date} date
82
 * @returns {Event}
83
 */
84
Event.prototype.setDate = function(date){
85
  if(date){
86
    this.date = GDate(date);
87
  }
88
  return this;
89
};
90
91
/**
92
 * Get the place
93
 * 
94
 * @returns {PlaceReference}
95
 */
96
Event.prototype.getPlace = function(){
97
  return this.place;
98
};
99
100
/**
101
 * Set the place
102
 * 
103
 * @param {PlaceReference} place
104
 * @returns {Event}
105
 */
106
Event.prototype.setPlace = function(place){
107
  if(place){
108
    this.place = PlaceReference(place);
109
  }
110
  return this;
111
};
112
113
/**
114
 * Get the roles
115
 * 
116
 * @returns {EventRole[]}
117
 */
118
Event.prototype.getRoles = function(){
119
  return this.roles || [];
120
};
121
122
/**
123
 * Set the roles
124
 * 
125
 * @param {EventRole[]|Object[]} roles
126
 * @returns {Event}
127
 */
128
Event.prototype.setRoles = function(roles){
129
  return this._setArray(roles, 'roles', 'addRole');
130
};
131
132
/**
133
 * Add a role
134
 * 
135
 * @param {EventRole|Object} role
136
 * @returns {Event}
137
 */
138
Event.prototype.addRole = function(role){
139
  return this._arrayPush(role, 'roles', EventRole);
140
};
141
142
/**
143
 * Export the object as JSON
144
 * 
145
 * @return {Object} JSON object
146
 */
147
Event.prototype.toJSON = function(){
148
  return this._toJSON(Subject, [
149
    'type',
150
    'date',
151
    'place',
152
    'roles'
153
  ]);
154
};
155
156
module.exports = Event;